﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using IconManager.ObjectModel;

namespace IconManager
{
	public partial class IconManagerDashboard : System.Web.UI.UserControl
	{
		/// <summary>
		/// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
		/// </summary>
		/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			// Configure controls
			this.iconPacksDropDownList.AutoPostBack = true;
			this.iconPacksDropDownList.SelectedIndexChanged += new EventHandler(this.IconPacksDropDownList_SelectedIndexChanged);

			this.selectedIconsRepeater.ItemDataBound += new RepeaterItemEventHandler(this.SelectedIconsRepeater_ItemDataBound);
		}

		/// <summary>
		/// Handles the Load event of the Page control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
			{
				this.iconPacksDropDownList.DataSource = Icons.GetPacks();
				this.iconPacksDropDownList.DataBind();

				this.availableIconsRepeater.DataSource = Icons.GetPackIcons(this.iconPacksDropDownList.SelectedValue);
				this.availableIconsRepeater.DataBind();
			}

			this.selectedIconsRepeater.DataSource = Icons.GetUmbracoIcons();
			this.selectedIconsRepeater.DataBind();
		}

		/// <summary>
		/// Resbuilds the avilableIconsRepeater for the newly selected IconCollection
		/// </summary>
		private void IconPacksDropDownList_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.availableIconsRepeater.DataSource = Icons.GetPackIcons(((DropDownList)sender).SelectedValue);
			this.availableIconsRepeater.DataBind();
		}


		/// <summary>
		/// Handles the ItemDataBound event of the SelectedIconsRepeater control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterItemEventArgs"/> instance containing the event data.</param>
		private void SelectedIconsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				// DataItem
				UmbracoIcon selectedUmbracoIcon = (UmbracoIcon)e.Item.DataItem;

				// Controls in the Repeater template
				HyperLink selectedIconHyperLink = (HyperLink)e.Item.FindControl("selectedIconHyperLink");
				Image selectedIconImage = (Image)e.Item.FindControl("selectedIconImage");
				Literal selectedIconLiteral = (Literal)e.Item.FindControl("selectedIconLiteral");

				// Configure controls
				selectedIconHyperLink.NavigateUrl = selectedUmbracoIcon.Url;

				if(selectedUmbracoIcon.InUse)
				{
					selectedIconHyperLink.CssClass = "in-use";
				}

				selectedIconImage.ImageUrl = selectedUmbracoIcon.Url;
				selectedIconImage.AlternateText = selectedUmbracoIcon.FileName;
				selectedIconImage.ToolTip = selectedUmbracoIcon.FileName;

				selectedIconLiteral.Text = selectedUmbracoIcon.Name; // FileName without the extension
			}
		}
	}
}